package utils.weixin;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utils.weixin.bean.AccessToken;
import utils.weixin.bean.Button;
import utils.weixin.bean.ClickButton;
import utils.weixin.bean.ComplexButton;
import utils.weixin.bean.Menu;
import utils.weixin.bean.UrlButton;



/**
 * 菜单管理器类
 * 
 */
public class MenuManager {
	private static Logger log = LoggerFactory.getLogger(MenuManager.class);

	public static void main(String[] args) throws UnsupportedEncodingException {
		// 第三方用户唯一凭证
		String appId = "wx95addf674a498ab9";
		// 第三方用户唯一凭证密钥
		String appSecret = "5aee0421f9ac8f330692371b03fa12c3";

		// 调用接口获取access_token
		AccessToken at = WeixinUtil.getAccessToken(appId, appSecret);

		if (null != at) {
			// 调用接口创建菜单
			int result = WeixinUtil.createMenu(getMenu(), at.getToken());

			// 判断菜单创建结果
			if (0 == result)
				log.info("菜单创建成功！");
			else
				log.info("菜单创建失败，错误码：" + result);
		}

	}

	/**
	 * 组装菜单数据
	 * 
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	private static Menu getMenu() throws UnsupportedEncodingException {
		ClickButton btn11 = new ClickButton();
		btn11.setName("查件");
		btn11.setType("click");
		btn11.setKey("query");

		/*
		 * ClickButton btn12 = new ClickButton(); btn12.setName("寄件");
		 * btn12.setType("click"); btn12.setKey("send");
		 */

		ClickButton btn12 = new ClickButton();
		btn12.setName("历史件查询");
		btn12.setType("click");
		btn12.setKey("history");

		// UrlButton btn13 = new UrlButton();
		// btn13.setName("用户注册");
		// btn13.setType("view");
		// btn13.setUrl("http://114.215.189.144:8080/youboxapia_debug?platformId=");

		/*
		 * ClickButton btn15 = new ClickButton(); btn15.setName("未绑定用户查件");
		 * btn15.setType("click"); btn15.setKey("ubindquery");
		 */

		UrlButton btn21 = new UrlButton();
		btn21.setName("天气查询");
		btn21.setType("view");
		btn21.setUrl("http://www.weather.com.cn/");

		UrlButton btn22 = new UrlButton();
		btn22.setName("物流查询");
		btn22.setType("view");
		btn22.setUrl("http://app.baidu.com/app/enter?appid=101074");
		
		UrlButton btn23 = new UrlButton();
		btn23.setName("我要洗衣");
		btn23.setType("view");
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95addf674a498ab9&redirect_uri=http%3a%2f%2fpublic.24box.cn%2fwxCallBack.jsp&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		//String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95addf674a498ab9&redirect_uri=http%3a%2f%2f114.215.189.144%3a8080%2fYouBoxService%2fwxCallBack.jsp&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		btn23.setUrl(url);
		
		UrlButton btn24 = new UrlButton();
		btn24.setName("早餐测试");
		btn24.setType("view");
		String url_zc = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95addf674a498ab9&redirect_uri=http%3a%2f%2fpublic.24box.cn%2fzcCallBack.jsp&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		//String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95addf674a498ab9&redirect_uri=http%3a%2f%2f114.215.189.144%3a8080%2fYouBoxService%2fwxCallBack.jsp&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		btn24.setUrl(url_zc);
		
		UrlButton btn25 = new UrlButton();
		btn25.setName("工作平台");
		btn25.setType("view");
		String url_login = "http://114.215.189.144:8080/YouBoxService/web2zc/login.jsp";
		//String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx95addf674a498ab9&redirect_uri=http%3a%2f%2f114.215.189.144%3a8080%2fYouBoxService%2fwxCallBack.jsp&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		btn25.setUrl(url_login);
		
		UrlButton btn30 = new UrlButton();
		btn30.setName("我要寄件");
		btn30.setType("view");
		btn30.setUrl("http://www.wenjuan.com/s/zmEVJv/?share=1&openid=obncMj1E9eL5k4YDFaXqeyo8RFJI&token=OezXcEiiBSKSxW0eoylIeAnLC0TfZK1JDA-rZgkbZfBpToqmb76njJSw9BdxdqnnUv3rluuvI0lN4yaF0GwV1mucoo8sCxpY1UfFZ4zltMmVFO3-jGW6Vnixomvlc8Ho3a9y8yL5Q7TxwbtYwp3neQ&bookid=default");

		UrlButton btn31 = new UrlButton();
		btn31.setName("关于我们");
		btn31.setType("view");
		btn31.setUrl("http://m.24box.cn/guanyuwomen.html");

		UrlButton btn32 = new UrlButton();
		btn32.setName("服务内容");
		btn32.setType("view");
		btn32.setUrl("http://m.24box.cn/anlizhanshi.html");

		UrlButton btn33 = new UrlButton();
		btn33.setName("联系我们");
		btn33.setType("view");
		btn33.setUrl("http://m.24box.cn/lanmu.html");

		UrlButton btn34 = new UrlButton();
		btn34.setName("移动官网");
		btn34.setType("view");
		btn34.setUrl("http://m.24box.cn/");
		


		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("邮宝服务");
		mainBtn1.setSub_button(new Button[] { btn11, btn12 });

//		ComplexButton mainBtn2 = new ComplexButton();
//		mainBtn2.setName("便民服务");
//		mainBtn2.setSub_button(new Button[] { btn21, btn22,btn23 });
		

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("便民服务");
		mainBtn2.setSub_button(new Button[] {btn23,btn24,btn25 });

		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("企业相关");
		mainBtn3.setSub_button(new Button[] { btn31, btn32, btn33, btn34 });
		

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是"更多体验"，而直接是"幽默笑话"，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		Menu menu = new Menu();
		//menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });
//		menu.setButton(new Button[] { mainBtn1, btn23, btn30 });//正式用的
		menu.setButton(new Button[] { mainBtn1, mainBtn2, btn30 });//2015.8.1测试用

		return menu;
	}
}